package com.none.web.controller.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import spring.mvc.bind.annotation.FormModel;

import com.none.core.common.utils.JSONUtil;
import com.none.core.common.utils.PathUtil;
import com.none.core.model.Pager;
import com.none.core.model.Result;
import com.none.web.common.SysConstant;
import com.none.web.model.TActivity;
import com.none.web.model.TActivityAppUser;
import com.none.web.model.TActivityImage;
import com.none.web.service.activity.IActivityService;


/**
 * 活动控制类Controller
 * @author Dave
 * @timer  20141106
 */

@Controller
@RequestMapping("activity")
public class ActivityController {
	@Resource(name = "activityService")
	IActivityService activityService;

	@Autowired
	private HttpServletRequest request;
	
	/**pc
     * 保存活动草稿
     * 1.可以添加草稿。2.可以修改草稿内容。3。也可以将已经发布的活动修改成草稿。
     * Dave20141107
     */
	@ResponseBody
	@RequestMapping("saveDraft")
	public Object saveDraft(@FormModel("activity") final TActivity activity,String startTime,String endTime,String deadline,String tags,String imageList_orderIndexs,String imageList_paths,String imageList_descs,String push,String pushcontent) throws Exception{
		//image list
		List<TActivityImage> imageList = new ArrayList<TActivityImage>();
		TActivityImage image = null;
		if(imageList_paths!=null && !imageList_paths.equals("")){
			String[] imageList_pathArr = imageList_paths.split(",,");
			String[] imageList_descArr = imageList_descs.split(",,");
			String[] imageList_orderIndexArr = imageList_orderIndexs.split(",,");
			for (int i = 0; i < imageList_pathArr.length; i++) {
				image = new TActivityImage();
				try{
				image.setOrderIndex(Integer.parseInt(imageList_orderIndexArr[i]));
				}catch(Exception e){
					image.setOrderIndex(0);
				}
				image.setImagePath(imageList_pathArr[i]);
				image.setDescription(imageList_descArr[i]);
				imageList.add(image);
			}
		}
		activity.setImageList(imageList);
		//
		String[] filter = new String[] { "appUsers", "TNewses", "activitys", "password"};
		String userId = request.getHeader("BasicAuthUsername");
		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.saveSuccess", data);
		if (!activityService.saveDraft(activity,startTime,endTime,deadline,tags,userId,push,request,pushcontent)) {
			result = new Result(SysConstant.STATE_ERROR, "activity.saveError", data);
		}
		return JSONUtil.toJSON(result, filter);
	}
	/**pc
     * 创建活动
     * 1.只能用于创建，不可以进行修改
     * Dave20141107
     */
	@ResponseBody
	@RequestMapping("create")
	public Object create(@FormModel("activity") final TActivity activity,String startTime,String endTime,String deadline, String tags,String push,String pushcontent) throws Exception{
		String[] filter = new String[] { "appUsers", "TNewses", "activitys", "password"};
		String userId = request.getHeader("BasicAuthUsername");
		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.saveSuccess", data);
		if (!activityService.createActivity(activity,startTime,endTime, deadline,tags, userId,push,request,pushcontent)) {
			result = new Result(SysConstant.STATE_ERROR, "activity.saveError", data);
		}

		return JSONUtil.toJSON(result, filter);
	}
	/**pc
     * 修改活动
     * 只是修改
     * 1. 发布者只能修改自己的活动，并且只能修改活动草稿和被拒绝的活动。
	 * 2. 审核着可以修改同部门的活动，但是不能修改同部门别人的活动草稿。
	 * 3. 需要更新修改人和修改时间
     */
	@ResponseBody
	@RequestMapping("edit")
	public Object edit(@FormModel("activity") final TActivity activity,String startTime,String endTime,String deadline, String tags,String push,String pushcontent)  throws Exception{
		String[] filter = new String[] { "appUsers", "TNewses", "activitys","tcodes","activity","appUser", "password"};

		String userId = request.getHeader("BasicAuthUsername");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("activityObj", activityService.editActivity(activity,startTime, endTime,deadline,tags, userId,push,request,pushcontent));
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.saveSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	/**
	 * 删除活动
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("delActivity")
	public Object delActivity(String activityId) throws Exception {
		String userId = request.getHeader("BasicAuthUsername");
		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.delSuccess", data);
		if (!activityService.deleteActivity(activityId,userId)) {
			result = new Result(SysConstant.STATE_ERROR, "activity.delError", data);
		}
		return JSONUtil.toJSON(result);
	}
	/**pc
     * 等待审核的活动
     * list
     */
	@ResponseBody
	@RequestMapping("listPending_review")
	public Object listPending_review(@FormModel("pager") final Pager pager) throws Exception {
		String[] filter = new String[] { "activitys", "TNewses","tcodes","appUsers", "password"};
		String userId = request.getHeader("BasicAuthUsername");

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("activity", activityService.listPending_review(pager, userId));   
		data.put("pager", pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.haveActivity", data);
		return JSONUtil.toJSON(result, filter);
	}
	/**
	 * pc
     * 审核活动 －通过
     */

	@ResponseBody
	@RequestMapping("review")
	public Object review(@FormModel("activity") final TActivity activity, String startTime,String endTime,String deadline,String tags,String push,String pushcontent) throws Exception {

		String[] filter = new String[] { "appUsers", "TNewses", "activitys","tcodes", "password"};

		String userId = request.getHeader("BasicAuthUsername");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("activity", activityService.review(activity, startTime, endTime, deadline,tags, userId,push,request,pushcontent));
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.updateSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}

	/**
	 * pc
     * 拒绝活动
     */
	@ResponseBody
	@RequestMapping("reject")
	public Object reject(String activityId) throws Exception {
		String[] filter = new String[] { "appUsers", "TNewses", "activitys","tcodes", "password"};

		String userId = request.getHeader("BasicAuthUsername");

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("activity", activityService.reject(activityId, userId,request));

		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.updateSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	/**pc
     * 搜索活动
     * 1.可以根据ID，主题，摘要内容搜索  2。根据时间段搜索创建时间   3。根据标签查询
     * list
     * Dave20141108
     */
	@ResponseBody
	@RequestMapping("search")
	public Object search(@FormModel("pager") final Pager pager,String keyWord,String startTime,String endTime,String tags,String status) throws Exception {
		String userId = request.getHeader("BasicAuthUsername");
		String[] filter = new String[] { "activitys", "TNewses","tcodes","appUsers", "password"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("activityObjs", activityService.search(pager, keyWord, startTime, endTime, tags, userId,status));
		data.put("pager", pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	/**pc
     * 正在进行中的活动
     * list
     *  Dave20141108
     */
	@ResponseBody
	@RequestMapping("ongoing")
	public Object ongoing() throws Exception {
		String userId = request.getHeader("BasicAuthUsername");
		String[] filter = new String[] {"tags","tcodes","activity","tactivityAppUsers", "password"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("activityObjs", activityService.ongoing(userId));
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	/**pc
     * 正在进行中的活动报名情况
     * list
     */
	@ResponseBody
	@RequestMapping("ongoing_view")
	public Object ongoing_view(@FormModel("pager") final Pager pager,String activityId) throws Exception {
		String userId = request.getHeader("BasicAuthUsername");
		String[] filter = new String[] { "tactivityAppUsers", "appUsers","activity","tcodes", "password"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("activityJoins", activityService.ongoing_view(pager, activityId,userId));
		data.put("pager", pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	/**pc
     * 导出某个活动的详细信息
     * param:exportType    如果值为“excel”就导出excel，如果是“txt”就导出txt文件，默认导出txt文件
     */
	@ResponseBody
	@RequestMapping("export")
	public Object export(String exportType,String activityId,HttpServletResponse response) throws Exception{
		//activityId="14154241523870000a1b36c1dca390a4";
		String userId = request.getHeader("BasicAuthUsername");
		String[] filter = new String[] { "tactivityAppUsers", "appUsers", "password"};
		Map<String, Object> data = new HashMap<String, Object>();
		
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.funcSuccess",data);
		if(!activityService.export(exportType, activityId,userId, request,response )){
			 result = new Result(SysConstant.STATE_ERROR, "activity.exportError",data);
		}
		return JSONUtil.toJSON(result,filter);
	}
	/**pc
     * 查询所有活动
     * 只有AdminConsole可以使用
     */
	@ResponseBody
	@RequestMapping("adminShow")
	public Object adminShow(@FormModel("pager") final Pager pager) throws Exception {
		String[] filter = new String[] { "appUsers", "TNewses", "activitys","tcodes", "password"};
		String userId = request.getHeader("BasicAuthUsername");

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("activity", activityService.adminShow(pager,userId));   
		data.put("pager", pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.queryAllSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	
	//app端
	
	/**app
     * 活动报名
     * Dave20141108
     */
	@ResponseBody
	@RequestMapping("joinActivity")
	public Object joinActivity(@FormModel("app") final TActivityAppUser app,String activityId,String userId)  throws Exception {
		String[] filter = new String[] { "appUsers", "tags","tactivityAppUsers", "password"};
		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}
		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.joinSuccess", data);
		if (!activityService.joinActivity(app, activityId,userId)) {
			result = new Result(SysConstant.STATE_ERROR, "activity.saveError", data);
		}	
		return JSONUtil.toJSON(result, filter);
	}
	/**app
     * 展示活动
	 * @throws Exception 
     */
	@ResponseBody
	@RequestMapping("show")
	public Object show(@FormModel("pager") final Pager pager, String keyWord, String tagNames,String userId) throws Exception {
		String[] filter = new String[] { "appUsers", "TNewses", "activitys","tcodes", "password"};
		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("activity", activityService.show(pager, keyWord, tagNames,userId));
		data.put("pager", pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.haveActivity", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	/**
	 * 获取活动详细信息
	 * 
	 * @param activityId
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("getDetialInfo")
	public Object getActivityDetialInfo(String activityId,String type,String appOrPCFlag,String userId) throws Exception {
		//String[] filter = new String[] { "TBeacons", "TNewses", "password", "lastTime", "tcodes","activitys"};
		String[] filter = new String[] { "appUsers", "TNewses", "activitys","tcodes","appUser", "password"};

		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}
		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "activity.haveActivity", data);

		data.put("activity", activityService.getActivityDetialInfo(activityId, type, userId,appOrPCFlag));
		return JSONUtil.toJSON(result, filter);
	}
	
}
